package com.zjuee.web.ledger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.constant.DictionaryConstants;
import com.zjuee.base.dto.common.ledger.LedgerSolidSubtotalDto;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.exception.ServiceException;
import com.zjuee.base.model.common.company.Company;
import com.zjuee.base.utils.DateUtils;
import com.zjuee.service.LedgerSolidSubtotalService;
import com.zjuee.service.company.service.CompanyService;
import com.zjuee.base.model.common.ledger.LedgerDispose;
import com.zjuee.base.model.common.ledger.LedgerProduce;
import com.zjuee.base.model.common.ledger.LedgerSolidSubtotal;
import com.zjuee.web.ledger.mapper.LedgerDisposeMapper;
import com.zjuee.web.ledger.mapper.LedgerProduceMapper;
import com.zjuee.web.ledger.mapper.LedgerSolidSubtotalMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author wu liang
 * @since 1.0
 */
@Slf4j
@Service
public class LedgerSolidSubtotalServiceImpl implements LedgerSolidSubtotalService {
    @Resource
    CompanyService companyService;
    @Resource
    LoginDto loginDto;
    @Resource
    LedgerProduceMapper ledgerProduceMapper;
    @Resource
    LedgerDisposeMapper ledgerDisposeMapper;
    @Resource
    LedgerSolidSubtotalMapper ledgerSolidSubtotalMapper;
    @Override
    public Long add(LedgerSolidSubtotal type) {
        return null;
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        return false;
    }

    @Override
    public boolean update(LedgerSolidSubtotal type) {
        return false;
    }

    @Override
    public LedgerSolidSubtotal selectById(Long id, Long validateId) {
        return null;
    }

    @Override
    public Page<LedgerSolidSubtotal> list(LedgerSolidSubtotalDto dto) {
        return null;
    }

    @Override
    public LambdaQueryWrapper<LedgerSolidSubtotal> fuzzyQuery(LedgerSolidSubtotalDto dto) {
        return null;
    }

    @Override
    public List<LedgerSolidSubtotal> selectList(LedgerSolidSubtotal ledgerSolidSubtotal) {
        return null;
    }

    @Override
    public void ledgerSolidProduceReport(LedgerProduce ledgerProduce, String state)  {
        // 需满足为固废或大宗固废
        if (DictionaryConstants.TYPE_SOLID.equals(ledgerProduce.getWlpType())
                || DictionaryConstants.TYPE_BSOLID.equals(ledgerProduce.getWlpType())) {
            LedgerSolidSubtotal ledgerSolidSubtotal = new LedgerSolidSubtotal();
            this.prepareProduceCondition(ledgerSolidSubtotal, ledgerProduce);
            // 类型：产生
            ledgerSolidSubtotal.setVlssType(1);

            if (DictionaryConstants.VL_LEDGER_NOMAL.equals(state)) {
                // 正常增加
                this.ledgerSolidSubAppend(ledgerProduce, ledgerSolidSubtotal);
            } else if (DictionaryConstants.VL_LEDGER_DELETE.equals(state)) {
                // 撤销扣除
                this.ledgerSolidSubRemove(ledgerProduce, ledgerSolidSubtotal);
            } else if (DictionaryConstants.VL_LEDGER_CHANGE.equals(state)) {
                // 数量更改
                this.ledgerSolidSubChange(ledgerProduce, ledgerSolidSubtotal);
            }
        }
    }

    @Override
    public void ledgerSolidDisposeReport(LedgerDispose ledgerDispose, String state)  {
        // 需满足为固废或大宗固废
        if (DictionaryConstants.TYPE_SOLID.equals(ledgerDispose.getWldType())
                || DictionaryConstants.TYPE_BSOLID.equals(ledgerDispose.getWldType())) {
            LedgerSolidSubtotal ledgerSolidSubtotal = new LedgerSolidSubtotal();
            this.prepareDisposeCondition(ledgerSolidSubtotal, ledgerDispose);
            // 类型：处置
            ledgerSolidSubtotal.setVlssType(2);

            if (DictionaryConstants.VL_LEDGER_NOMAL.equals(state)) {
                // 正常增加
                this.ledgerSolidSubAppend(ledgerDispose, ledgerSolidSubtotal);
            } else if (DictionaryConstants.VL_LEDGER_DELETE.equals(state)) {
                // 撤销扣除
                this.ledgerSolidSubRemove(ledgerDispose, ledgerSolidSubtotal);
            } else if (DictionaryConstants.VL_LEDGER_CHANGE.equals(state)) {
                // 数量更改
                this.ledgerSolidSubChange(ledgerDispose, ledgerSolidSubtotal);
            }

        }
    }
    private LedgerSolidSubtotal prepareProduceCondition(LedgerSolidSubtotal ledgerSolidSubtotal, LedgerProduce ledgerProduce) {
        // 获取产生企业信息
        Company company = companyService.selectById(ledgerProduce.getBciId(),null).getCompany();
        ledgerSolidSubtotal.setVlssProvince(company.getBciProvince());
        ledgerSolidSubtotal.setVlssCity(company.getBciCity());
        ledgerSolidSubtotal.setVlssArea(company.getBciArea());
        ledgerSolidSubtotal.setVlssStreet(company.getBciStreet());
        ledgerSolidSubtotal.setVlssSolidType(ledgerProduce.getWlpType());
        ledgerSolidSubtotal.setVlssCode(ledgerProduce.getWlpBcode());

        return ledgerSolidSubtotal;
    }

    private LedgerSolidSubtotal prepareDisposeCondition(LedgerSolidSubtotal ledgerSolidSubtotal, LedgerDispose ledgerDispose) {
        // 获取处置企业信息
        Company company = companyService.selectById(loginDto.getOrgBusinessId(),null).getCompany();
        ledgerSolidSubtotal.setVlssProvince(company.getBciProvince());
        ledgerSolidSubtotal.setVlssCity(company.getBciCity());
        ledgerSolidSubtotal.setVlssArea(company.getBciArea());
        ledgerSolidSubtotal.setVlssStreet(company.getBciStreet());
        ledgerSolidSubtotal.setVlssSolidType(ledgerDispose.getWldType());
        ledgerSolidSubtotal.setVlssCode(ledgerDispose.getWldBcode());

        return ledgerSolidSubtotal;
    }

    private void ledgerSolidSubAppend(LedgerProduce ledgerProduce, LedgerSolidSubtotal ledgerSolidSubtotal) {
        ledgerSolidSubtotal.setVlssYear(String.valueOf(DateUtils.getYear(new Date())));
        ledgerSolidSubtotal.setVlssMonth(String.valueOf(DateUtils.getMonth(new Date())));
        List<LedgerSolidSubtotal> ledgerSolidSubtotalList = ledgerSolidSubtotalMapper.selectList(ledgerSolidSubtotal);

        if (!CollectionUtils.isEmpty(ledgerSolidSubtotalList)) {
            LedgerSolidSubtotal ledgerSolid = ledgerSolidSubtotalList.get(0);
            BigDecimal scale = ledgerSolid.getVlssScale();
            ledgerSolid.setVlssScale(scale.add(ledgerProduce.getWlpNumber()));
            this.update(ledgerSolid);
        } else {
            LedgerSolidSubtotal ledgerSolid = new LedgerSolidSubtotal();
            BeanUtils.copyProperties(ledgerSolidSubtotal, ledgerSolid);
            ledgerSolid.setVlssScale(ledgerProduce.getWlpNumber());
            ledgerSolidSubtotalMapper.insert(ledgerSolid);
        }
    }

    private void ledgerSolidSubRemove(LedgerProduce ledgerProduce, LedgerSolidSubtotal ledgerSolidSubtotal)  {
        ledgerSolidSubtotal.setVlssYear(String.valueOf(DateUtils.getYear(ledgerProduce.getWlpCreateTime())));
        ledgerSolidSubtotal.setVlssMonth(String.valueOf(DateUtils.getMonth(ledgerProduce.getWlpCreateTime())));

        List<LedgerSolidSubtotal> ledgerSolidSubtotalList = ledgerSolidSubtotalMapper.selectList(ledgerSolidSubtotal);

        if (!CollectionUtils.isEmpty(ledgerSolidSubtotalList)) {
            LedgerSolidSubtotal ledgerSolid = ledgerSolidSubtotalList.get(0);
            BigDecimal scale = ledgerSolid.getVlssScale();
            ledgerSolid.setVlssScale(scale.subtract(ledgerProduce.getWlpNumber()));
            this.update(ledgerSolid);
        } else {
            // 无数据时修改，数据会出现错误
            throw new ServiceException("数据错误!");
        }
    }

    private void ledgerSolidSubChange(LedgerProduce ledgerProduce, LedgerSolidSubtotal ledgerSolidSubtotal)  {
        ledgerSolidSubtotal.setVlssYear(String.valueOf(DateUtils.getYear(ledgerProduce.getWlpCreateTime())));
        ledgerSolidSubtotal.setVlssMonth(String.valueOf(DateUtils.getMonth(ledgerProduce.getWlpCreateTime())));
        LedgerProduce oldLedgerProduce = ledgerProduceMapper.selectById(ledgerProduce.getId());
        List<LedgerSolidSubtotal> ledgerSolidSubtotalList = ledgerSolidSubtotalMapper.selectList(ledgerSolidSubtotal);

        // 有历史记录，且有相关的统计数据
        if (oldLedgerProduce != null && !CollectionUtils.isEmpty(ledgerSolidSubtotalList)) {
            LedgerSolidSubtotal ledgerSolid = ledgerSolidSubtotalList.get(0);
            BigDecimal oldScale = oldLedgerProduce.getWlpNumber();
            BigDecimal scale = ledgerSolid.getVlssScale();
            // 减去旧数据，添加新数据
            ledgerSolid.setVlssScale(scale.subtract(oldScale).add(ledgerProduce.getWlpNumber()));
            this.update(ledgerSolid);
        } else {
            // 无数据时修改，数据会出现错误
            throw new ServiceException("数据错误!");
        }
    }

    private void ledgerSolidSubAppend(LedgerDispose ledgerDispose, LedgerSolidSubtotal ledgerSolidSubtotal) {
        ledgerSolidSubtotal.setVlssYear(String.valueOf(DateUtils.getYear(new Date())));
        ledgerSolidSubtotal.setVlssMonth(String.valueOf(DateUtils.getMonth(new Date())));

        List<LedgerSolidSubtotal> ledgerSolidSubtotalList = ledgerSolidSubtotalMapper.selectList(ledgerSolidSubtotal);

        if (!CollectionUtils.isEmpty(ledgerSolidSubtotalList)) {
            LedgerSolidSubtotal ledgerSolid = ledgerSolidSubtotalList.get(0);
            BigDecimal scale = ledgerSolid.getVlssScale();
            ledgerSolid.setVlssScale(scale.add(ledgerDispose.getWldNumber()));
            this.update(ledgerSolid);
        } else {
            LedgerSolidSubtotal ledgerSolid = new LedgerSolidSubtotal();
            BeanUtils.copyProperties(ledgerSolidSubtotal, ledgerSolid);
            ledgerSolid.setVlssScale(ledgerDispose.getWldNumber());
            ledgerSolidSubtotalMapper.insert(ledgerSolid);
        }
    }

    private void ledgerSolidSubRemove(LedgerDispose ledgerDispose, LedgerSolidSubtotal ledgerSolidSubtotal) {
        ledgerSolidSubtotal.setVlssYear(String.valueOf(DateUtils.getYear(ledgerDispose.getWldCreateTime())));
        ledgerSolidSubtotal.setVlssMonth(String.valueOf(DateUtils.getMonth(ledgerDispose.getWldCreateTime())));

        List<LedgerSolidSubtotal> ledgerSolidSubtotalList =ledgerSolidSubtotalMapper.selectList(ledgerSolidSubtotal);

        if (!CollectionUtils.isEmpty(ledgerSolidSubtotalList)) {
            LedgerSolidSubtotal ledgerSolid = ledgerSolidSubtotalList.get(0);
            BigDecimal scale = ledgerSolid.getVlssScale();
            ledgerSolid.setVlssScale(scale.subtract(ledgerDispose.getWldNumber()));
            this.update(ledgerSolid);
        } else {
            // 无数据时修改，数据会出现错误
            throw new ServiceException("数据错误!");
        }
    }

    private void ledgerSolidSubChange(LedgerDispose ledgerDispose, LedgerSolidSubtotal ledgerSolidSubtotal)  {
        ledgerSolidSubtotal.setVlssYear(String.valueOf(DateUtils.getYear(ledgerDispose.getWldCreateTime())));
        ledgerSolidSubtotal.setVlssMonth(String.valueOf(DateUtils.getMonth(ledgerDispose.getWldCreateTime())));

        LedgerDispose oldLedgerDispose = ledgerDisposeMapper.selectById(ledgerDispose.getId());
        List<LedgerSolidSubtotal> ledgerSolidSubtotalList = ledgerSolidSubtotalMapper.selectList(ledgerSolidSubtotal);

        // 有历史记录，且有相关的统计数据
        if (oldLedgerDispose != null && !CollectionUtils.isEmpty(ledgerSolidSubtotalList)) {
            LedgerSolidSubtotal ledgerSolid = ledgerSolidSubtotalList.get(0);
            BigDecimal oldScale = oldLedgerDispose.getWldNumber();
            BigDecimal scale = ledgerSolid.getVlssScale();
            // 减去旧数据，添加新数据
            ledgerSolid.setVlssScale(scale.subtract(oldScale).add(oldLedgerDispose.getWldNumber()));
            this.update(ledgerSolid);
        } else {
            // 无数据时修改，数据会出现错误
            throw new ServiceException("数据错误!");
        }
    }
}
